package ntut.andengine.game;

import java.util.ArrayList;
import java.util.List;

import ntut.andengine.physics.Bar;
import ntut.andengine.physics.BarSwitch;
import ntut.andengine.physics.ContactEvent;
import ntut.andengine.physics.DeathLight;
import ntut.andengine.physics.PhysicalComponent;
import ntut.andengine.physics.DeathLight.Direction;
import ntut.andengine.physics.PhysicalComponent.ComponentColor;
import ntut.andengine.physics.PhysicalFactory;

import org.anddev.andengine.engine.handler.IUpdateHandler;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.bitmap.BitmapTexture;
import org.anddev.andengine.opengl.texture.bitmap.BitmapTextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TextureRegion;

import android.widget.Toast;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;

public class GameLevel7 extends GameLevel{
	
	GameLevel mLevel;
	Bar bar1;
    Bar bar2;
    BarSwitch barSwitch;
	
	@Override
	public void onLoadResources() {
		super.onLoadResources();
		mLevel = this;
		
		BitmapTexture backgroundBitmapTexture = new BitmapTexture(512, 1024, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		TextureRegion backgroundTextureRegion = BitmapTextureRegionFactory.createFromAsset(backgroundBitmapTexture, this, "universe.png", 0, 0);
		mEngine.getTextureManager().loadTexture(backgroundBitmapTexture);		
		Sprite background = new Sprite(0, 0, backgroundTextureRegion);
		mScene.attachChild(background);
	}
	
	@Override
	public Scene onLoadScene() {

        PhysicalComponent gate = PhysicalFactory.CreateTransmissionGate(this, new Vector2(73, 349));
        gate.getBody().setUserData(new ContactEvent("Gate"){

			@Override
			public void beginContact(Body body, Vector2 Normal) {
				
				// LEVEL COMPLETE!!
				if(body.getUserData().toString() == "Player"){
					mLevel.runOnUiThread(new Runnable(){

						@Override
						public void run() {
							mMusic.pause();
							successSound.play();
							Toast.makeText(mLevel, "Level Completed!", Toast.LENGTH_SHORT).show();	
						}
			        	
			        });
					
					startActivity(GameLevel7.this, GameAbout.class);
				}	
			}

			@Override
			public void endContact(Body body, Vector2 Normal) {
				// TODO Auto-generated method stub
				
			}
		
		});

        PhysicalFactory.CreateWall(this, new Vector2(40, 703), 80, 20, ComponentColor.White);
        PhysicalFactory.CreateWall(this, new Vector2(143, 690), 20, 220, ComponentColor.White);
        PhysicalFactory.CreateWall(this, new Vector2(334, 749), 240, 20, ComponentColor.White);
        PhysicalFactory.CreateWall(this, new Vector2(334, 449), 20, 460, ComponentColor.White);
        PhysicalFactory.CreateWall(this, new Vector2(440, 374), 80, 20, ComponentColor.White);
        PhysicalFactory.CreateWall(this, new Vector2(444, 609), 20, 260, ComponentColor.White);
        PhysicalFactory.CreateWall(this, new Vector2(384, 229), 80, 20, ComponentColor.White);
        PhysicalFactory.CreateWall(this, new Vector2(430, 160), 100, 20, ComponentColor.White);
        PhysicalFactory.CreateWall(this, new Vector2(330, 40), 20, 80, ComponentColor.White);
        PhysicalFactory.CreateWall(this, new Vector2(400, 90), 160, 20, ComponentColor.White);
        PhysicalFactory.CreateWall(this, new Vector2(53, 80), 20, 160, ComponentColor.White);
        PhysicalFactory.CreateWall(this, new Vector2(123, 312), 20, 300, ComponentColor.White);
        PhysicalFactory.CreateWall(this, new Vector2(173, 152), 120, 20, ComponentColor.White);
        PhysicalFactory.CreateWall(this, new Vector2(61, 299), 120, 20, ComponentColor.White);
        PhysicalFactory.CreateWall(this, new Vector2(184, 472), 280, 20, ComponentColor.White);
        PhysicalFactory.CreateWall(this, new Vector2(222, 382), 20, 160, ComponentColor.White);
        PhysicalFactory.CreateWall(this, new Vector2(54, 552), 20, 140, ComponentColor.White);
        PhysicalFactory.CreateWall(this, new Vector2(134, 534), 140, 20, ComponentColor.White);
        PhysicalFactory.CreateWall(this, new Vector2(214, 584), 20, 120, ComponentColor.White);
        
        PhysicalFactory.CreateBox(this, new Vector2(280, 132), ComponentColor.Red);
        PhysicalFactory.CreateBox(this, new Vector2(280, 132), ComponentColor.Blue);
        
        List<DeathLight> deatLightList = PhysicalFactory.CreateDeathLight(this, new Vector2(392, 400), 160, 40, Direction.Right);
        PhysicalFactory.CreateSwitchDevice(this, new Vector2(299, 50), true, deatLightList);  
        PhysicalFactory.CreateLaunchingPad(this, new Vector2(272, 402), true);

        List<Bar> barList = new ArrayList<Bar>();
        bar1 = PhysicalFactory.CreateBar(this, new Vector2(300, 500), ComponentColor.Red, new Vector2(0, 0), true);
        bar2 = PhysicalFactory.CreateBar(this, new Vector2(300, 500), ComponentColor.Blue, new Vector2(0, 0), true);
        barList.add(bar1);
        barList.add(bar2);
        barSwitch = PhysicalFactory.CreateBarSwitch(this, new Vector2(155, 205), true, barList);//153, 202
        
        mSparkle = PhysicalFactory.CreateSparkle(this, new Vector2(200, 129), ComponentColor.Blue);//73, 129
	
		this.mScene.registerUpdateHandler(this.mPhysicsWorld);
		mScene.registerUpdateHandler(new IUpdateHandler(){
			
			@Override
			public void onUpdate(float pSecondsElapsed) {
				
				CheckPutBack() ;
			}

			@Override
			public void reset() {
				// TODO Auto-generated method stub
				
			}
			
		});
		
		return mScene;
	}

	private void CheckPutBack() {
        
		
		if(barSwitch.isOn() && mSparkle.getPosition().x < 300 && mSparkle.getPosition().y > 420 && mSparkle.getPosition().y < 580){
		
			bar1.getBody().setActive(true);
			bar2.getBody().setActive(true);
			bar1.setVelocity(new Vector2(-5, 0));
			bar2.setVelocity(new Vector2(-5, 0));
			
			if (bar1.getPosition().x < 0){
	            bar1.setPosition(new Vector2(300, 500));
	        }
	
	        if (bar2.getPosition().x < 0){
	            bar2.setPosition(new Vector2(300, 500));
	        }
		}
		else{
			bar1.getBody().setActive(false);
			bar2.getBody().setActive(false);
		}
    }
}
